<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="userProfile.photo"
            @click="$refs.fileRef.click()"
          />
          <input
            type="file"
            accept="image/*"
            @change="onFileChange"
            v-show="false"
            ref="fileRef"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="userProfile.name"
        @click="onNameCellClick"
      />
      <van-cell
        title="生日"
        is-link
        :value="userProfile.birthday"
        v-on:click="onBirthCellClick"
      />
    </van-cell-group>

    <van-dialog
      v-model="showNameDialog"
      title="修改名称"
      show-cancel-button
      :before-close="onBeforeClose"
    >
      <van-field
        v-model="name"
        placeholder="请输入名称"
        maxlength="15"
        input-align="center"
        ref="nameRef"
      />
    </van-dialog>

    <van-action-sheet v-model="showBirthSheet">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthSheet = false"
        @confirm="changeBirthDay"
      />
    </van-action-sheet>
  </div>
</template>

<script>
import {
  getUserProfileAPI,
  uploadAvatorAPI,
  updateUserProfileAPI
} from '@/api/userAPI'
import dayjs from 'dayjs'
export default {
  data() {
    return {
      userProfile: {},
      name: '',
      showNameDialog: false,
      showBirthSheet: false,
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(2080, 10, 1),
      currentDate: new Date()
    }
  },

  components: {},
  created() {
    this.getUserProfile()
  },

  computed: {},

  mounted() {},

  methods: {
    async getUserProfile() {
      // 请求用户介绍的信息
      const { data: res } = await getUserProfileAPI()
      this.userProfile = res.data
    },
    async onFileChange(e) {
      const files = e.currentTarget.files
      if (files.length === 0) return
      const currentImg = files[0]
      const fd = new FormData()
      fd.append('photo', currentImg)
      const { data: res } = await uploadAvatorAPI(fd)
      if (res.message === 'OK') {
        this.getUserProfile()
        this.$notify({ type: 'primary', message: '图片上传成功' })
      }
    },
    onNameCellClick() {
      this.name = this.userProfile.name
      this.showNameDialog = true

      this.$nextTick(() => {
        this.$refs.nameRef.focus()
      })
    },
    async onBeforeClose(action, done) {
      console.log('action:', action)
      if (action === 'cancel') return done()
      if (action === 'confirm') {
        if (this.name === '') {
          done(false)
          this.$notify({
            type: 'warning',
            message: '名称不能为空',
            duration: 2000
          })
          this.$refs.nameRef.focus()
          return
        }
        const { data: res } = await updateUserProfileAPI({ name: this.name })
        console.log('Res:', res)
        if (res.message === 'OK') {
          done()
          this.getUserProfile()
          this.$notify({
            type: 'success',
            message: '更新成功！',
            duration: 2000
          })
        }
      }
    },
    onBirthCellClick() {
      if (this.userProfile.birthday) {
        this.currentDate = new Date(this.userProfile.birthday)
      }
      this.showBirthSheet = true
    },
    async changeBirthDay(value) {
      this.showBirthSheet = false
      const { data: res } = await updateUserProfileAPI({
        birthday: dayjs(value).format('YYYY-MM-DD')
      })
      if (res.message === 'OK') {
        this.getUserProfile()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.user-edit-container {
  padding-top: 92px;
}

.user-edit-container {
  padding-top: 92px;
  .avatar {
    width: 100px;
    height: 100px;
  }
}

// 如何修改组件的样式
.van-nav-bar /deep/ .van-icon {
  color: #fff;
}
</style>
